package com.example.star.controller;

import com.example.star.entity.BuyNow;
import com.example.star.entity.Song;
import com.example.star.service.SongService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;
import java.util.List;

@Controller
public class SongController {

    private static final Logger logger = LoggerFactory.getLogger(SongController.class);

    @Autowired
    private SongService songService;

    @PostMapping("/saveSong")
    @ResponseBody
    public Song saveBuyNow(@RequestBody Song song) {
        songService.saveSong(song);
        return song; // 返回保存的song对象
    }

    @GetMapping("/checkSongName")
    @ResponseBody
    public boolean checkSongName(@RequestParam("songname") String songname) {
        logger.debug("检查歌曲名是否存在: {}", songname);
        return songService.isSongNameExists(songname);
    }

    // 显示歌曲页面
    @GetMapping("/songlist")
    public String showSongList(Model model) {
        logger.info("用户访问歌曲列表页面");
        List<Song> songs = songService.getAllSongs();
        model.addAttribute("songs", songs);
        return "songlist";
    }

    @ResponseBody
    @GetMapping("/songs")
    public ResponseEntity<List<Song>> getSongs() {
        logger.info("收到获取所有歌曲的请求");
        List<Song> songs = songService.getAllSongs();
        return new ResponseEntity<>(songs, HttpStatus.OK);
    }

    @ResponseBody
    @GetMapping("/songs/bySinger")
    public ResponseEntity<List<Song>> getSongsBySingerName(@RequestParam("singername") String singername) {
        logger.info("收到按歌手名 {} 获取歌曲的请求", singername);
        List<Song> songs = songService.getSongsBySingerName(singername);
        return new ResponseEntity<>(songs, HttpStatus.OK);
    }


}